package com.stottlerhenke.simbionic.samples.helloworld;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.ArrayList;

import com.stottlerhenke.simbionic.api.SB_Config;
import com.stottlerhenke.simbionic.api.SB_Engine;
import com.stottlerhenke.simbionic.api.SB_Error;

public class HelloWorld
{
	 public static void main(String[] args)
	  {
	    HelloWorld_Interface helloWorld_Interface1 = new HelloWorld_Interface();

	    // create the object that holds configuration parameters for the engine
	    SB_Config myConfig = new SB_Config();
	    
	    myConfig.debugConnectTimeout = 90;
	    myConfig.debugEnabled = false;

	    // specify the name of the SIM file to load
	    try
	    {
	    	File file = new File("samples/HelloWorld/HelloWorld.sim");
			myConfig.fileURL = new URL("file", "localhost", file.getAbsolutePath());
	    }
	    catch(Exception e)
	    {
	      System.out.println("Exception creating URL from the sim file name.");
	    }

	    // creates an instance of the engine interface object
	    SB_Engine engine = new SB_Engine();

	   // specify the log file
	    PrintStream logPrintStream = null;
	    try
	    {
	      logPrintStream = new PrintStream( new FileOutputStream("HelloWorld.log") );
	    }
	    catch(Exception ex)
	    {
	      ex.printStackTrace();
	    }

	    if( logPrintStream != null )
	    {
	      engine.registerLogPrintStream( logPrintStream );
	    }

	    // initialize the engine
	    if (engine.initialize(helloWorld_Interface1,myConfig) != SB_Error.kOK)
	    {
	      System.out.println("Engine failed to initialize: " + engine.getLastError());
	      return;
	    }

	    // create an entity
	    long friendlyGuy = engine.makeEntity( "Friendly Guy" );
	    if (friendlyGuy == -1 )
	    {
	      System.out.println("Entity creation error: " + engine.getLastError());
	      return;
	    }

	    // set Friendly Guy's initial behavior
	    SB_Error errCode = engine.setBehavior( friendlyGuy, "Main", new ArrayList());
	    if (errCode != SB_Error.kOK)
	    {
	      System.out.println("Error setting behavior: " + engine.getLastError());
	      return;
	    }

	    // update the entity several times
	    for (int tick=0; tick < 5; tick++)
	    {
	      if (engine.update() != SB_Error.kOK)
	      {
	        System.out.println("Update error: " + engine.getLastError());
	        return;
	      }
	    }

	    engine.terminate();     // shut down the engine

	  }

}
